from rest_framework import serializers
from django.contrib.auth.models import User
from .models import Task


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'first_name', 'last_name']


class TaskSerializer(serializers.ModelSerializer):
    assigned_to = UserSerializer(read_only=True)
    assigned_to_id = serializers.PrimaryKeyRelatedField(
        queryset=User.objects.all(),
        source='assigned_to',
        write_only=True,
        required=False,
        allow_null=True
    )
    created_by = UserSerializer(read_only=True)

    class Meta:
        model = Task
        fields = [
            'id', 'title', 'description', 'status',
            'Projectname', 'system',
            'ProjectManager', 'ProductManager', 'DevelopmentEngineer', 'TestEngineer',
            'Demandlink', 'Testlink',
            'startdate', 'enddate', 'due_date', 'testschedule', 'testprogress',
            'Picturedata', 'Remarks',
            'created_at', 'updated_at', 'assigned_to', 'assigned_to_id', 'created_by','Testingprocess'
        ]
        read_only_fields = ['created_at', 'updated_at', 'created_by']